package com.lhy.controller;

import com.lhy.domain.DoctorAdvice;
import com.lhy.domain.Doctors;
import com.lhy.domain.User;
import com.lhy.service.DoctorService;
import com.lhy.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class DoctorController {
    @Autowired
    DoctorService doctorService;
    @RequestMapping("/showAllDoctors")
    public String showAllDoctors(HttpServletRequest req, HttpServletResponse resp){
        List<Doctors> doctors=doctorService.showAllDoctors();
        System.out.println(doctors);
        HttpSession session = req.getSession();
        session.setAttribute("doctors",doctors);
        return "yslist";
    }
    @RequestMapping("/addDoctor")
    public String addDoctor(Doctors doctors,User u,HttpServletRequest req, HttpServletResponse resp){
        System.out.println(doctors);
        System.out.println(u);
        doctorService.addDoctor(doctors,u);
        return "main";
    }
    @RequestMapping("/deleteDoctor")
    public String deleteDoctor(HttpServletRequest req,HttpServletResponse resp){
        Integer docId = Integer.parseInt(req.getParameter("docId"));
        doctorService.deleteDoctor(docId);
        return "main";
    }
    @RequestMapping("/getDoctorId")
    public String getDoctorId(HttpServletRequest req,HttpServletResponse resp){
        Integer docId = Integer.parseInt(req.getParameter("docId"));
        HttpSession session= req.getSession();
        session.setAttribute("docId",docId);
        return "ysmodify";
    }
    @RequestMapping("/modifyDoctor")
    public String modifyDoctor(HttpServletRequest req,HttpServletResponse resp){
        Map<String, String[]> parameterMap = req.getParameterMap();
        Doctors doctors=new Doctors();
        try {
            BeanUtils.populate(doctors,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        doctorService.modifyDoctor(doctors);
        return "main";
    }
    @RequestMapping("/findSomeDoctors")
    public String findSomeDoctors(Pagination<Doctors> page, Doctors doctor, HttpServletRequest req, HttpServletResponse resp){
        page.setQueryObject(doctor);
        System.out.println(doctor);
        doctorService.findSomeDoctor1(page);
        HttpSession session=req.getSession();
        session.setAttribute("list",page);
        return "yslist";
    }

}
